package com.example.androidgeneric;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.example.androidgeneric.ServerJsonResult.onServerRequest;
import com.google.gson.reflect.TypeToken;

public class F_ResultTest extends Fragment implements onServerRequest<Coupon> {

	private static final String URL = "http://www.sharping.co.kr/rmobile/select_coupon_user_free.php";
	private static final String SEND_POST_PARAM_NAME = "UserId";
	
	private TextView text;

	private ServerJsonResult<Coupon> server_result;
	
	private RequestQueue mQueue;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
		if (mQueue == null) {
			mQueue = Volley.newRequestQueue(getActivity());
		}
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_main, null);
		text = (TextView) rootView.findViewById(R.id.textview);

		return rootView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		setCouponData();
	}

	private void setCouponData() {
		
		
		HashMap<String, String> sendParam = new HashMap<String, String>();
		
		sendParam.put(SEND_POST_PARAM_NAME, "p1001@appilog.co.kr");
		Type type = new TypeToken<ArrayList<Coupon>>(){}.getType();
		server_result = new ServerJsonResult<Coupon>(URL, mQueue, type);
		server_result.setParam(sendParam);
		server_result.setOnServerRequest(this);
		server_result.commit();
		
		
	}

	@Override
	public void onResult(Coupon result) {
		text.setText(result.getCCouponBizName());
	}

	@Override
	public void onStartConnectionUI() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onEndConnection() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onError(VolleyError err) {
		// TODO Auto-generated method stub
	}
}
